package com.dyc.caradvertisting.ui.fmt.tab.first;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dyc.caradvertisting.R;
import com.dyc.caradvertisting.base.BaseLazyMainFragment;
import com.dyc.caradvertisting.event.StartBrotherEvent;
import com.dyc.caradvertisting.ui.fmt.setting.SettingFmt;
import com.dyc.caradvertisting.ui.fmt.tab.first.messge.InputFragment;
import com.dyc.caradvertisting.ui.fmt.tab.first.messge.MessageFragment;
import com.dyc.caradvertisting.ui.fmt.tab.first.messge.PhraseFragment;
import com.dyc.caradvertisting.ui.fmt.tab.smsg.SystemMesFmt;
import com.dyc.caradvertisting.ui.fmt.trav.TravFmt;
import com.dyc.caradvertisting.widget.TabSwitchView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 首页
 */
public class FirstHomeFragment extends BaseLazyMainFragment {


    @BindView(R.id.txt_input)
    TextView txtInput;
    @BindView(R.id.txt_line_input)
    TextView txtLineInput;
    @BindView(R.id.line_input)
    LinearLayout lineInput;
    @BindView(R.id.txt_phrase)
    TextView txtPhrase;
    @BindView(R.id.txt_line_phrase)
    TextView txtLinePhrase;
    @BindView(R.id.line_phrase)
    LinearLayout linePhrase;
    @BindView(R.id.txt_message)
    TextView txtMessage;
    @BindView(R.id.txt_line_message)
    TextView txtLineMessage;
    @BindView(R.id.line_message)
    LinearLayout lineMessage;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.fmt_message)
    FrameLayout fmtMessage;
    @BindView(R.id.toolbar_x_car)
    TextView toolbarXCar;
    @BindView(R.id.img_setting)
    ImageView imgSetting;
    @BindView(R.id.toolbar_title_tv)
    TextView toolbarTitleTv;

    public FirstHomeFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static FirstHomeFragment newInstance() {
        FirstHomeFragment fragment = new FirstHomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_first_home;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        inflater.inflate(R.menu.menu_main_fragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_setting:
                // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
                EventBus.getDefault().post(new StartBrotherEvent(SettingFmt.newInstance()));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void initView(View baseView) {
        ButterKnife.bind(this, baseView);
        initToolbar(R.string.title_first);
        setHasOptionsMenu(true);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    }

    @Override
    public boolean isSwipeBack() {
        return false;
    }

    @Override
    public void initPresenter() {
//        mPresenter.setVM(this, mModel, (SupportActivity) this.getBaseActivty());
    }

    public void setSwitchTab(int id) {
        lineInput.setSelected(false);
        linePhrase.setSelected(false);
        lineMessage.setSelected(false);
        txtLineMessage.setVisibility(View.GONE);
        txtLineInput.setVisibility(View.GONE);
        txtLinePhrase.setVisibility(View.GONE);
        switch (id) {
            case R.id.line_input:
                lineInput.setSelected(true);
                txtLineInput.setVisibility(View.VISIBLE);
                loadRootFragment(R.id.fmt_message, InputFragment.newInstance());
                break;
            case R.id.line_phrase:
                linePhrase.setSelected(true);
                txtLinePhrase.setVisibility(View.VISIBLE);
                loadRootFragment(R.id.fmt_message, PhraseFragment.newInstance());
                break;
            case R.id.line_message:
                lineMessage.setSelected(true);
                txtLineMessage.setVisibility(View.VISIBLE);
                loadRootFragment(R.id.fmt_message, MessageFragment.newInstance());
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ButterKnife.bind(this, getView(inflater, container));
        return getView(inflater, container);
    }

    @Override
    protected void initLazyView(@Nullable Bundle savedInstanceState) {
        lineInput.setSelected(true);
        loadRootFragment(R.id.fmt_message, InputFragment.newInstance());
    }

    @OnClick({R.id.line_input, R.id.line_phrase, R.id.line_message,
            R.id.toolbar_x_car, R.id.img_setting})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.line_input:
            case R.id.line_phrase:
            case R.id.line_message:
                setSwitchTab(view.getId());
                break;
            case R.id.toolbar_x_car:
                // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
                EventBus.getDefault().post(new StartBrotherEvent(TravFmt.newInstance()));
                break;
            case R.id.img_setting:
                // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
                EventBus.getDefault().post(new StartBrotherEvent(SettingFmt.newInstance()));
                break;
        }
    }

}
